# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=helix
pkgver=22.05
pkgrel=0
pkgdesc="A post-modern modal text editor"
url="https://helix-editor.com/"
arch="aarch64 armhf armv7 ppc64le x86 x86_64"  # blocked by rust/cargo
license="MPL-2.0"
makedepends="cargo"
checkdepends="tree-sitter-rust"
install="$pkgname.post-install"
# This is needed just for tests.
_tsrust_ver="0.20.0"
source="https://github.com/helix-editor/helix/archive/$pkgver/helix-$pkgver.tar.gz
	https://github.com/tree-sitter/tree-sitter-rust/archive/v$_tsrust_ver/tree-sitter-rust-$_tsrust_ver.tar.gz
	runtime-dir-fhs-location.patch
	ungit.patch
	"

# Reduce the binary size - 9.6 -> 5.4 MiB (v0.6.0).
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
export CARGO_PROFILE_RELEASE_LTO="true"
export CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
export CARGO_PROFILE_RELEASE_PANIC="abort"

# Don't build tree-sitter languages, we provide them in standalone aports.
export HELIX_DISABLE_AUTO_GRAMMAR_BUILD=1

prepare() {
	default_prepare

	# This is needed for running tests.
	mkdir -p runtime/grammars/sources
	ln -s /usr/lib/tree-sitter/rust.so runtime/grammars/
	ln -s "$srcdir"/tree-sitter-rust-$_tsrust_ver runtime/grammars/sources/rust

	cargo fetch --locked
}

build() {
	cargo build --frozen --release
}

check() {
	cargo test --frozen --workspace
}

package() {
	install -D -m755 target/release/hx -t "$pkgdir"/usr/bin/

	install -d -m755 "$pkgdir"/usr/share/$pkgname
	cp -r runtime "$pkgdir"/usr/share/$pkgname/

	rm -rf "$pkgdir"/usr/share/$pkgname/runtime/grammars
	ln -Ts ../../../lib/tree-sitter "$pkgdir"/usr/share/$pkgname/runtime/grammars
}

sha512sums="
72bf17002f9d4ea9484e4ea2a65f5190fbc6528bc3da921065cf9277ad22943f3b97bdd4a478b39af6dfcfd6c9d6acb40ec91de4e55d0a744055719ed0bbfeb6  helix-22.05.tar.gz
1c7283e973f50989980a5a05217d664e87665c5ea2338978deaf0cf88f624c45fde8162848c68bb3b8836d32cbf9be80b1da6fa7aa03fa3cab8d8efb49e3694c  tree-sitter-rust-0.20.0.tar.gz
2b372ab19c6b30d5edb0ff9084bfd67ce339ea76583347214f827b41ce9caeb4fac48ea9f8dc5497e9c0cfe05fc13b758711a9ec0a76cbe459f406e47fce2a23  runtime-dir-fhs-location.patch
229119f877a4270ca543df6a00e4acc160dc8dfc28c27b28eff22999d66cdc650ed74f12c28cf114a1bd5536f010fbfaab02fc3eefb301a966758bcb6817bb18  ungit.patch
"
